import request from "../utils/request";
import fetch from "dva/fetch";

export const online = `http://8085.sobeylingyun.com/`; //`http://39.105.117.74:8085`; //配置线上地址
// export const online = `https://nmpx.nmgcyy.com.cn/`; 
let webUrl;
if (window.location.host.indexOf("localhost") > -1) {
    webUrl = online;
} else {
    webUrl = `${document.location.protocol}//${window.location.host}`;
}
export const UrlConfig = webUrl;
export function requestOther(url, options) {
    let token = window.localStorage.getItem("token");

    if (!options) {
        options = {
            method: "get",
            headers: { "Content-Type": "application/json; charset=utf-8" }
        };
    }
    options.headers.Token = token;
    options.headers.System = "tianma";
    return fetch(url, options)
        .then(checkStatus)
        .then(parseJSON)
        .then(data => ({ data }))
        .catch(err => ({ err }));
}

export const call = function (url, method = "get", data = {}) {
    url = UrlConfig + url;
    if (method == "get") {
        let urls = "?";
        for (const key in data) {
            if (data.hasOwnProperty(key)) {
                urls = `${urls}${key}=${data[key]}&`;
            }
        }
        url = url + urls;
        return request(url);
    } else if (method == "post") {
        return request(url, {
            method: "post",
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            body: JSON.stringify(data)
        });
    } else if (method == "delete") {
        return request(url, {
            method: "post",
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            body: JSON.stringify(data)
        });
    }
};

export const callOther = function (baseUrl, access_key, url, method = "get", data = {}) {
    url = baseUrl + url;
    if (method == "get") {
        let urls = "?";
        for (const key in data) {
            if (data.hasOwnProperty(key)) {
                urls = `${urls}${key}=${data[key]}&`;
            }
        }
        url = url + urls;
        let options = {
            method: "get",
            headers: {
                "Content-Type": "application/json; charset=utf-8",
                "Access-key": access_key
            }
        };
        return requestOther(url, options);
    } else if (method == "post") {
        return requestOther(url, {
            method: "post",
            headers: {
                "Content-Type": "application/json; charset=utf-8",
                "Access-key": access_key
            },
            body: JSON.stringify(data)
        });
    }
};
const option = function (data) {
    return {
        method: "post",
        headers: {
            "Content-Type": "application/json; charset=utf-8"
        },
        body: JSON.stringify(data)
    };
};
export function getVersion(data) {
    //获取版本配置
    return call("/fcmatrix/version/index", "get", data);
}
/**
 * 获取分类 版面列表
 */
export function getTypeList() {
    let token = localStorage.getItem("token"); //获取当前用户授权的版面
    let data = {
        type: "admin",
        token: token ? token : ""
    };
    return call("/fcinformation/Plate/plateList", "get", data);
}
export function getVideoPlateList() {
    //http://39.105.117.74:8085/fcvideo/Plate/plateList?version=1&token=8D7559F5EED936EB9320FD8496D79BC9&
    return call("/fcvideo/Plate/plateList", "get", {
        version: 1,
        type:'admin'
    });
}
/**
 * 获取分类 已发布版面列表
 */
export function getlistTypePlate() {
    let token = localStorage.getItem("token"); //获取当前用户授权的版面
    let data = {
        type: "admin",
        token: token ? token : ""
    };
    return call("/fcinformation/Plate/listTypePlate", "get", data);
}

/**
 * 获取模板列表
 */
export function getModalList() {
    return call("/fcinformation/Template/templateList");
}
/**
 * 删除单个分类
 * id  分类id
 */
export function deleteType(id) {
    return call("/fcinformation/Plate/deletePlate", "post", id);
}
/**
 * 新增分类
 * method post
 * data
 */
export function addType(data) {
    return call("/fcinformation/Plate/addPlate", "post", data);
}
/**
 * 修改分类
 * method post
 * data
 */
export function editType(data) {
    return call("/fcinformation/Plate/updatePlate", "post", data);
}
/**
 * 分类排序
 */
export function sortType(data) {
    return call("/fcinformation/Plate/changeSort", "post", data);
}
/**
 * 获取资讯列表  搜索
 */
export function getVideoList(data) {
    let index = data.index ? data.index : "",
        plate_id = data.plate_id ? data.plate_id : "",
        page_size = data.page_size ? data.page_size : "10",
        status = data.status ? data.status : "",
        state = data.state ? data.state : "",
        class_id = data.class_id ? data.class_id : "",
        key = data.key ? data.key : "",
        start = data.start ? data.start : "",
        types = data.types ? data.types : "",
        information_ids = data.information_ids ? data.information_ids : "",
        module_name = data.module_name ? data.module_name : "",
        end = data.end ? data.end : "",
        start_state = data.start_state || '',
        end_state = data.end_state,
        editor = data.editor || '',
        author = data.author || '',
        check_user = data.check_user || '',
        create_user = data.create_user || ''

    return request(
        UrlConfig +
        "/fcinformation/Information/informationList?is_admin=1&index=" +
        index +
        "&module_name=" +
        module_name +
        "&plate_id=" +
        plate_id +
        "&page_size=" +
        page_size +
        "&start=" +
        start +
        "&status=" +
        status +
        "&key=" +
        key +
        "&state=" +
        state +
        "&end=" +
        end +
        "&class_id=" +
        class_id +
        "&information_ids=" +
        information_ids +
        "&types=" +
        types +
        '&start_state=' +
        start_state +
        '&end_state=' +
        end_state +
        '&editor=' +
        editor +
        '&author=' +
        author +
        '&check_user=' +
        check_user +
        '&create_user=' +
        create_user
    );
}
/**
 * 获取视频列表  搜索
 */
export function getVideoListVideo(data) {
    let index = data.index ? data.index : "",
        plate_id = data.plate_id ? data.plate_id : "",
        page_size = data.page_size ? data.page_size : "10",
        status = data.status ? data.status : "",
        state = data.state ? data.state : "",
        key = data.key ? data.key : "",
        start = data.start ? data.start : "",
        end = data.end ? data.end : "";
    return request(
        UrlConfig +
        "/fcvideo/Video/videoList?is_admin=1&index=" +
        index +
        "&plate_id=" +
        plate_id +
        "&page_size=" +
        page_size +
        "&start=" +
        start +
        "&status=" +
        status +
        "&key=" +
        key +
        "&state=" +
        state +
        "&end=" +
        end
    );
}

/**
 * 删除视频稿件
 */
export function deleteVideo(data) {
    return call("/fcinformation/Information/deleteInformation", "post", data);
}
/**
 * 上下架视频
 */
export function upDownVideo(data) {
    return call("/fcinformation/Information/changeState", "post", data);
}
/**
 * 审核视频状态
 */
export function auditVideo(data) {
    return call("/fcinformation/Information/changeStatus", "post", data);
}
/**
 * 获取新增视频
 */
export function addVideo(data) {
    return call("/fcvideo/Video/addVideo", "post", data);
}

/**
 * 获取全局配置列表
 */
export function getConfig() {
    return call("/fcinformation/AppConfig/getConf", "get", {
        config_id: 1
    });
}
/**
 * 修改全局配置
 */
export function updateConfig(data) {
    return call("/fcinformation/AppConfig/replaceConf", "post", data);
}
/**
 * 获取评论配置列表
 */
export function getCommentConfig() {
    return call("/fcinformation/Config/getConfigList", "get", {});
}
/**
 * 修改评论配置
 */
export function updateCommentConfig(data) {
    return call("/fcinformation/Config/batchUpdateConfig", "post", data);
}
/**
 * 获取评论列表
 */
export function getAllComment(data) {
    return call("/fcinformation/Comment/getAllComment", "get", data);
}

/**
 * 删除评论
 */
export function deleteComment(data) {
    return call("/fcinformation/Comment/deleteComment", "post", data);
}

/**
 * 审核评论
 */
export function checkComment(data) {
    return call("/fcinformation/Comment/checkComment", "post", data);
}
/**
 * 获取一级评论列表
 */
export function commentList(data) {
    return call("/fcinformation/Comment/commentList", "get", data);
}

/**
 * 获取二级评论列表
 */
export function commentDetail(data) {
    return call("/fcinformation/Comment/commentDetail", "get", data);
}

/**
 * 获取评论列表
 */
export function getAllThemeComment(data) {
    return call("/fcinformation/ThemeComment/getAllComment", "get", data);
}

/**
 * 删除评论
 */
export function deleteThemeComment(data) {
    return call("/fcinformation/ThemeComment/deleteComment", "post", data);
}

/**
 * 审核评论
 */
export function checkThemeComment(data) {
    return call("/fcinformation/ThemeComment/checkComment", "post", data);
}
/**
 * 获取一级评论列表
 */
export function themeCommentList(data) {
    return call("/fcinformation/ThemeComment/commentList", "get", data);
}

/**
 * 获取二级评论列表
 */
export function themeCommentDetail(data) {
    return call("/fcinformation/ThemeComment/commentDetail", "get", data);
}
/**
 * 回复评论
 */
export function addAdminComment(data) {
    return call("/fcinformation/comment/addAdminComment", "post", data);
}
/*
 *移动到不同分类
 */
export function moveToOther(data) {
    return call("/fcinformation/Information/moveInformation", "post", data);
}
/**
 * @Author: 戴文锋 daiwenfeng@sobey.com
 * @msg: http://fengchao.cn/fcinformation/Information/updateInformationPlate
 * @param  {
      plate_information_ids: [],
      plate_id: []
    }
 * @return:
 */
export function batchEditPlate(data) {
    return call(
        "/fcinformation/Information/updateInformationPlate",
        "post",
        data
    );
}

/**
 * 排序接口
 */
export function sortTable(data) {
    return call("/fcinformation/Information/sortInformation", "post", data);
}
/**
 * 获取用户信息
 */
export function loginUser(data) {
    return call("/fcinformation/Login/login", "get", data);
}

/**
 * 修改稿件
 */
export function updateVideo(data) {
    return call("/fcinformation/Information/updateInformation", "post", data);
}

/**
 * 获取资讯详情
 */
export function videoInfo(data) {
    return call("/fcinformation/Information/informationInfo", "get", data);
}

/**
 * 获取资讯列表
 */
export function videoList(data) {
    data.is_admin = 1;
    return call("/fcinformation/Information/informationList", "get", data);
}

/**
 * 新增资讯
 */
export function addInformation(data) {
    let token = window.localStorage.getItem("token");

    let site_id = window.localStorage.getItem("site_id");
    data.token = token;
    data.site_id = site_id;
    return call("/fcinformation/Information/addInformation", "post", data);
}

/**
 * 获取资讯历史
 * @param {资讯id} data
 */
export function getInfoHistory(data) {
    return call("/fcinformation/Information/getLogDataList", "get", data);
}

/**
 * 获取资讯历史版本详情(预览)
 * @param {资讯历史版本id} data
 */
export function getInfoHistoryYulan(data) {
    return call("/fcinformation/Information/getLogDataInfo", "get", data);
}

/**
 * 获取资讯历史版本详情
 * @param {资讯历史版本id} data
 */
export function getInfoHistoryDetail(data) {
    return call("/fcinformation/information/compareLogData", "get", data);
}


/**
 * 更新资讯历史
 * @param {资讯历史版本id,备注remarks} data
 */
export function updateLogData(data) {
    return call("/fcinformation/information/updateLogData", "get", data);
}



/**
 * 删除资讯历史版本
 * @param {资讯历史版本id} data
 */
export function deleteInfoHistory(data) {
    return call("/fcinformation/Information/delLogDataInfo", "get", data);
}

/**
 * 获取分类列表  new type
 */
export function classList(data) {
    return call("/fcinformation/Infoclass/classList", "get", data);
}

/**
 * 新增分类  new type
 */
export function addInfoclass(data) {
    return call("/fcinformation/Infoclass/addInfoclass", "post", data);
}

/**
 * 修改分类  new type
 */
export function updateInfoclass(data) {
    return call("/fcinformation/Infoclass/updateInfoclass", "post", data);
}

/**
 * 删除分类  new type
 */
export function deleteInfoclass(data) {
    return call("/fcinformation/Infoclass/deleteInfoclass", "post", data);
}

/**
 * 获取资讯稿件详情
 */
export function informationInfo(data) {
    return call("/fcinformation/Information/informationInfo", "get", data);
}

/**
 * 引用
 */
export function usedModule(data) {
    return call("/fcinformation/Information/usedModule", "post", data);
}

/**
 * 获取视频分类列表
 */
export function getTreeListVideo(data) {
    let token = localStorage.getItem("token")
        ? localStorage.getItem("token")
        : ""; //获取当前用户授权的版面
    if (data) {
        data.token = token;
    } else {
        data = {
            token: token
        };
    }
    data.type = 'admin';
    return call("/fcvideo/Plate/plateList", "get", data);
}

/**
 * 置顶
 */
export function top(data) {
    return call("/fcinformation/Information/top", "post", data);
}

/**
 * 取消置顶
 */
export function cancelTop(data) {
    return call("/fcinformation/Information/cancelTop", "post", data);
}

/**
 * 获取视频模板列表
 */
export function videoTemplateList(data) {
    let token = localStorage.getItem("token")
        ? localStorage.getItem("token")
        : ""; //获取当前用户授权的版面
    if (data) {
        data.token = token;
    } else {
        data = {
            token: token
        };
    }
    data.type = 'admin';
    return call("/fcvideo/Plate/plateList", "get", data);
}

/**
 * 发现列表
 */
export function serviceList(data) {
    return call("/fcfind/Service/serviceList", "get", data);
}

/**
 * 直播列表
 */
export function liveList(baseUrl, access_key, data) {
    return callOther(baseUrl, access_key, "/live/v1/live/lists", "get", data);
}

/**
 * 专辑列表
 */
export function videothemeList(data) {
    return call("/fcvideo/Theme/themeList", "get", data);
}
/**
 * 获取视频详情
 */
export function getVideoInfo(data) {
    return call("/fcvideo/video/videoInfo", "get", data);
}

/**
 * 获取视频详情
 */
export function getThemeinformation(data) {
    return call("/fcinformation/Themeinformation/informationInfo", "get", data);
}

/**
 * 获取服务版面分类
 */
export function getFcfind(data) {
    return call("/fcfind/Plate/plateList", "get", data);
}

/**
 * 资讯推送
 */
export function fcinfoPush(data) {
    return call("/fcinformation/Push/addPush", "post", data);
}

/**
 * 上传base64图片
 */
export function uploadImgBase64(data) {
    return call("/fcinformation/Information/uploadImgBase64", "post", data);
}

/**
 * 获取天马爬虫数据
 */
export function getMocData(data) {
    return call("/fcinformation/information/getMocList", "get", data);
}

/**
 * 获取天马爬虫数据
 */
export function getTmData(data) {
    return call("/fcinformation/Api/getTmData", "get", data);
}

/**
 * 角色绑定版面
 */
export function RoleBindPlate(data) {
    return call("/fcinformation/User/RoleBindPlate", "post", data);
}

/**
 * 获取全部分类 版面列表
 */
export function getAllTypeList() {
    let data = {
        type: "admin"
    };
    return call("/fcinformation/Plate/plateList", "get", data);
}
/**
 * 全局批量上下架
 */
export function changeState(data) {
    return call("/fcinformation/Information/changeState", "post", data);
}
/**
 * 全局批量删除资讯
 */
export function deleteInformation(data) {
    return call("/fcinformation/Information/deleteInformation", "post", data);
}

/**
 * 敏感词过滤
 */
export function validateSensitive(data) {
    return call("/fcinformation/Sensitivewords/validateSensitive", "post", data);
}

/**
 * 微信矩阵公众号列表
 */
export function weixinMpList(url) {
    // return request(url+'/mp/api/mpList');
    return fetch(url + "/mp/api/mpList", {})
        .then(checkStatus)
        .then(parseJSON)
        .then(data => ({
            data
        }))
        .catch(err => ({
            err
        }));
}
/**
 * 微信矩阵公众号列表
 */
export function weixinPush(url, data) {
    return fetch(url + "/app/material/api/create_news", option(data))
        .then(checkStatus)
        .then(parseJSON)
        .then(data => ({
            data
        }))
        .catch(err => ({
            err
        }));

    // return request(url+'/app/material/api/create_news',option(data));
}

function parseJSON(response) {
    return response.json();
}

function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        return response;
    }

    const error = new Error(response.statusText);
    error.response = response;
    throw error;
}

/**
 * 上传服务器图片到oss
 */
export function uploadOssImg(data) {
    return call("/fcpublic/uploads/aliyunossUploadImg", "post", data);
}

/**
 * 视频入库
 */
export function inStock(data) {
    return call("/fcpublic/Megateway/import", "post", data);
}

/**
 * 添加视频到视频组件
 */
export function addToVideo(data) {
    return call("/fcpublic/fcvideo/addVideo", "post", data);
}

/**
 * 提交阿里云转码
 */
export function transcode(data) {
    return call("/fcpublic/Aliyun/transcode", "post", data);
}
/**
 * 获取行政区域列表
 */
export function getAreaList() {
    return call("/fcinformation/area/index", "get");
}
/**
 * 更新简介
 */
export function updateDesc(data) {
    return call("/fcinformation/area/update", "post", data);
}


// 爬虫数据列表
export function crawlerList(data) {
    return call("/fcinformation/Information/infoCrawlerList", "get", data);
}


// 获取敏感词
export function sensitiveWords(data) {
    return call("/fcinformation/Sensitivewords/index", "get", data);
}

// 添加敏感词
export function addSensitiveWords(data) {
    return call("/fcinformation/Sensitivewords/save", "post", data);
}

// 删除敏感词
export function delSensitiveWords(data) {
    return call("/fcinformation/Sensitivewords/delete", "delete", data);
}

//获取一键排版配置信息
export function getStyleConfig(data) {
    return call("/fcinformation/appConfig/getStyleConfig", "get", data);
}

//更新一键排版配置信息
export function updateStyleConfig(data) {
    return call("/fcinformation/appConfig/updateStyleConfig", "post", data);
}

// 修改资讯发布时间
export function changeStateTime(data) {
    return call("/fcinformation/information/changeStateTime", "post", data);
}

// 直播版面列表
export function banmianList() {
    let B_url;
    if (localStorage.getItem('config_json')) {
        B_url = JSON.parse(localStorage.getItem('config_json')).saas_domain
    } else {
        B_url = 'http://testsaas.sobeylingyun.com'
    }
    let url = B_url + '/live/v1/plate/plateListForTM'
    let options = {
        method: 'get',
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            token: localStorage.getItem('token'),
            'access-key': JSON.parse(localStorage.getItem('config_json')).access_key,
            system: 'tianma'
        }
    };
    return fetch(url, options)
        .then(checkStatus)
        .then(parseJSON)
        .then(data => ({ data }))
        .catch(err => ({ err }));
}

// 带货列表
export function goodsList(limit) {
    let B_url = 'http://testsaas.sobeylingyun.com';
    if (localStorage.getItem('config_json')) {
      B_url = JSON.parse(localStorage.getItem('config_json')).saas_domain
    } else {
      B_url = 'http://testsaas.sobeylingyun.com'
    }
    if (B_url) {
      let url = B_url + `/ad/api/goods?page=1&limit=${limit}`;
      let ak = '';
      if(localStorage.getItem('config_json')){
          ak = JSON.parse(localStorage.getItem('config_json')).access_key;
      }

      let options = {
        method: 'get',
        headers: {
          'Content-Type': 'application/json; charset=utf-8',
          token: localStorage.getItem('token'),
          'access-key': ak,
          system: 'tianma'
        }
      };

      return fetch(url, options)
      .then(checkStatus)
      .then(parseJSON)
      .then(data => ({ data }))
      .catch(err => ({ err }));
    }


  }


/**
 * 新建水印模板
 */
export function addWaterMark(data) {
    return call("/fcpublic/rule/save", "post", data);
}


/**
 * 编辑提交水印模板
 */
export function editWaterMark(data) {
    return call("/fcpublic/rule/update", "post", data);
}

/**
 * 水印模板详情
 */
export function detailWaterMark(data) {
    return call("/fcpublic/rule/read", "post", data);
}

/**
 * 删除水印模板
 */
export function deleteWaterMark(data) {
    return call("/fcpublic/rule/delete", "post", data);
}

/**
 * 水印模板列表
 */
export function waterMarkList(data) {
    return call("/fcpublic/rule/index", "post", data);
}
